{% extends "base.html" %}

{% block content %}
<div class="container-fluid">
    <div class="card mb-4">
        <div class="card-header">
            <h5 class="mb-0"><i class="fas fa-search me-2"></i>查询条件</h5>
        </div>
        <div class="card-body">
            <form method="GET" action="{{ url_for('water.water_index') }}" class="row g-3">
                <div class="col-md-4">
                    <label for="device_id" class="form-label">设备ID</label>
                    <input type="text" class="form-control" id="device_id" name="device_id" value="{{ search_device_id }}">
                </div>
                <div class="col-md-4">
                    <label for="name" class="form-label">设备名称</label>
                    <input type="text" class="form-control" id="name" name="name" value="{{ search_name }}">
                </div>
                <div class="col-md-4">
                    <label for="location" class="form-label">位置</label>
                    <input type="text" class="form-control" id="location" name="location" value="{{ search_location }}">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-search me-1"></i>查询
                    </button>
                    <a href="{{ url_for('water.water_index') }}" class="btn btn-secondary">
                        <i class="fas fa-redo me-1"></i>重置
                    </a>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <h5 class="mb-0"><i class="fas fa-tint me-2"></i>水质监测</h5>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table">
                    <thead>
                        <tr>
                            <th>设备ID</th>
                            <th>设备名称</th>
                            <th>位置</th>
                            <th>pH值</th>
                            <th>浊度</th>
                            <th>溶解氧</th>
                            <th>电导率</th>
                            <th>温度</th>
                            <th>最后更新时间</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for device in devices %}
                        <tr>
                            <td>{{ device.device_id }}</td>
                            <td>{{ device.name }}</td>
                            <td>{{ device.location }}</td>
                            {% if device.id in latest_records %}
                            <td>{{ "%.2f"|format(latest_records[device.id].ph) }}</td>
                            <td>{{ "%.2f"|format(latest_records[device.id].turbidity) }}</td>
                            <td>{{ "%.2f"|format(latest_records[device.id].dissolved_oxygen) }}</td>
                            <td>{{ "%.2f"|format(latest_records[device.id].conductivity) }}</td>
                            <td>{{ "%.1f"|format(latest_records[device.id].temperature) }}℃</td>
                            <td>{{ latest_records[device.id].timestamp.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            {% else %}
                            <td colspan="6" class="text-center">暂无数据</td>
                            {% endif %}
                            <td>
                                <a href="{{ url_for('water.water_record', device_id=device.id) }}" class="btn btn-sm btn-primary">
                                    <i class="fas fa-plus me-1"></i>记录
                                </a>
                                <a href="{{ url_for('water.water_history', device_id=device.id) }}" class="btn btn-sm btn-info">
                                    <i class="fas fa-history me-1"></i>历史
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- 分页 -->
            {% if pagination and pagination.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    <li class="page-item {% if not pagination.has_prev %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('water.water_index', page=pagination.prev_num, device_id=search_device_id, name=search_name, location=search_location) if pagination.has_prev else '#' }}">
                            <i class="fas fa-chevron-left"></i>
                        </a>
                    </li>
                    {% for page in pagination.iter_pages() %}
                        {% if page %}
                            <li class="page-item {% if page == pagination.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('water.water_index', page=page, device_id=search_device_id, name=search_name, location=search_location) }}">{{ page }}</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled">
                                <span class="page-link">...</span>
                            </li>
                        {% endif %}
                    {% endfor %}
                    <li class="page-item {% if not pagination.has_next %}disabled{% endif %}">
                        <a class="page-link" href="{{ url_for('water.water_index', page=pagination.next_num, device_id=search_device_id, name=search_name, location=search_location) if pagination.has_next else '#' }}">
                            <i class="fas fa-chevron-right"></i>
                        </a>
                    </li>
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %} 